package util;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * Composes CSV data for collection of objects. Relays on theirs {@link Object#toString toString()} implementation.<br>
 * Could use memory buffer for the data or write straight to the output stream.
 * <p>
 * Example 1:
 * 
 * <pre>
 * Object[][] data = ...
 * 
 * CsvWriter writer = new CsvWriter();
 * writer.write("Column 1", "Column 2", ... "Column N");
 * for (Object[] values : data)
 *      writer.write(values);
 * ...
 * System.out.println(writer.toString());
 * </pre>
 * <p>
 * Example 2:
 * 
 * <pre>
 * Object[][] data = ...
 * 
 * CsvWriter writer = null;
 * try { 
 *      writer = new CsvWriter(new FileOutputStream(new File("data.csv")));
 *      writer.write("Column 1", "Column 2", ... "Column N");
 *      for (Object[] values : data)
 *          writer.write(values);
 * } finally {
 *      writer.close();
 * }
 * </pre>
 * 
 * @author Mykhaylo Adamovych
 */
public class CsvWriter implements Closeable {
    public static final String NULL_MARK = "";
    public static final String QUOTE = "\"";
    private String nullMark = NULL_MARK;
    private final PrintWriter pw;
    private ByteArrayOutputStream baos;
    
    /**
     * Creates temporary buffer in the memory. <br>
     * Use {@link #toString()} thereafter. No need to close.
     */
    public CsvWriter() {
        baos = new ByteArrayOutputStream();
        pw = new PrintWriter(baos, true);
    }
    
    /**
     * Doesn't consume memory for CSV data, writes straight to the output stream. Just like {@link FilterOutputStream}, but deal with objects and relays on
     * theirs {@link Object#toString toString()} implementation.<br>
     * Close writer thereafter.
     * 
     * @param os
     *            output stream to write data.
     */
    public CsvWriter(OutputStream os) {
        pw = new PrintWriter(os, true);
    }
    
    protected String composeRecord(Object... values) {
        if (values == null || values.length == 0)
            return "";
        final StringBuffer csvRecord = new StringBuffer();
        csvRecord.append(QUOTE);
        csvRecord.append(composeValue(values[0]));
        csvRecord.append(QUOTE);
        for (int i = 1; i < values.length; i++) {
            csvRecord.append("," + QUOTE);
            csvRecord.append(composeValue(values[i]));
            csvRecord.append(QUOTE);
        }
        return csvRecord.toString();
    }
    
    protected String composeValue(Object value) {
        if (value == null)
            return nullMark;
        return value.toString().replaceAll(QUOTE, QUOTE + QUOTE);
    }
    
    public String getNullMark() {
        return nullMark;
    }
    
    public void setNullMark(String nullMarker) {
        nullMark = nullMarker;
    }
    
    @Override
    public String toString() {
        if (baos == null)
            throw new UnsupportedOperationException();
        return baos.toString();
    }
    
    /**
     * Writes collection of objects as CSV record.
     * 
     * @param values
     */
    public void write(Collection<?> values) {
        write(values.toArray());
    }
    
    /**
     * Writes collection of objects as CSV record.
     * 
     * @param values
     */
    public void write(Object... values) {
        pw.println(composeRecord(values));
    }
    
    @Override
    public void close() throws IOException {
        if (baos != null)
            throw new UnsupportedOperationException();
        pw.close();
    }
}
